<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>待开票记录</title>

    <!--引入 element-ui 的样式，-->
    <link rel="stylesheet" href="/admin/plugin/element-ui/index.css">
    <!-- 必须先引入vue，  后使用element-ui -->
    <script type="text/javascript" src="/admin/js-css/js/vue.min.js"></script>
    <!-- 引入element 的组件库-->
    <script type="text/javascript" src="/admin/plugin/element-ui/index.js"></script>

    <link rel="stylesheet" href="/admin/js-css/css/backColor.css" media="all">


    <script type="text/javascript" src="/admin/js-css/js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="/admin/js-css/js/mx1.0.js?v=1.1"></script>

</head>
<body>
<div id="app">
    <template>
        <el-table
                :data="listdata"
                border
                style="width: 100%">
            <el-table-column
                    fixed="right"
                    label="操作"
                    width="100">
                <template slot-scope="scope">
                    <el-button @click="handleClick(scope.row)" type="text" size="small">开票</el-button>
                </template>
            </el-table-column>


            <el-table-column
                    prop="fpmc"
                    label="抬头名称"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="gfkpdz"
                    label="开票地址"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="gfkpdh"
                    label="开票电话">
            </el-table-column>

            <el-table-column
                    prop="gfkhh"
                    label="开户行">
            </el-table-column>
            <el-table-column
                    prop="gfkhhzh"
                    label="开户行账号">
            </el-table-column>
            <el-table-column
                    prop="gfdh"
                    label="通知电话">
            </el-table-column>
            <el-table-column
                    prop="spmc"
                    label="商品名称">
            </el-table-column>
            <el-table-column
                    prop="dj"
                    label="单价">
            </el-table-column>
            <el-table-column
                    prop="sl"
                    label="数量">
            </el-table-column>
            <el-table-column
                    prop="fee"
                    label="金额">
            </el-table-column>
            <el-table-column
                    prop="shuilv"
                    label="税率">
            </el-table-column>

        </el-table>
    </template>
</div>

</body>

<script>
    var vm = new Vue({
        el: '#app',
        data: {
            parmsData:{},
            listdata:[]
        }
    });


    getdata();
    function getdata(){
        let param = {
            "mapdata": {},
            "tableId": "baicheng-1938"
        }
        postdata("/admin/api/CommonDataProcessingController/postCustom",param,function (res){
            console.log(res);
            if(res.code == 0){
                window.location.href = "/admin/html/module/baicheng/jiaofei_list/login.html";
            }else {
                vm.listdata = res.data;
            }
        });


    }

    function handleClick(item){
        var res = Invoice(item);
        if(res){
            let param = {
                "mapdata": {id:item.id},
                "tableId": "baicheng-1939"
            }
            postdata("/admin/api/CommonDataProcessingController/postCustom",param,function (res){
                alert(res.msg);
                getdata();
            })
        }
    }

    function postdata(url,data,succ){
        $.ajax({
            type: "POST",
            url: url,
            headers: {
                token:localStorage.getItem("token")
            },
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(data),
            success: function (jsonResult) {
                succ(jsonResult);
            }
        });
    }


    function  OpenCard(){  //打开税金盘
        var a = new ActiveXObject("TaxCardX.GoldTax");
        a.CertPassWord = "12345678";
        a.OpenCard();
        console.log("a.RetCode",a.RetCode);
        if(a.RetCode=="1011" || a.RetCode=="3001"){
            this.is_open=1;
            this.CheckCode = a.CheckCode;
            console.log("金税卡开启成功！");
            return true;
        }else{
            alert("金税卡开启失败，错误代码："+a.RetCode+ "---"+a.RetMsg);
            return false;
        }
    }


    function CloseCard(){ //关闭税金盘
        var a = new ActiveXObject("TaxCardX.GoldTax");
        a.CloseCard();
        console.log("金税卡关闭成功！");
    }

    function Invoice(options){
        console.log(options);
        // debugger ;
        var a = new ActiveXObject("TaxCardX.GoldTax");
        if(!OpenCard()){
            return false;
        }
        try{
            let invoiceType = 51;
            if(getQuantity(invoiceType) <= 0){
                CloseCard();
                return false;
            }

            a.InfoKind=invoiceType;
            a.InfoClientName=options.fpmc;//购方名称
            a.InfoClientTaxCode=options.gfqysh;// - 购方税号

            if(options.gfkhh && options.gfkhhzh){
                a.InfoClientBankAccount=options.gfkhh + " " + options.gfkhhzh;// - 购方开户行及账号
            }else{
                a.InfoClientBankAccount="";
            }

            if(options.gfkpdz && options.gfkpdh){
                a.InfoClientAddressPhone=options.gfkpdz + " " + options.gfkpdh;// - 购方地址电话
            }else{
                a.InfoClientAddressPhone="";// - 购方地址电话
            }
            a.InfoSellerBankAccount=options.khh + " " + options.khhzh;// -　销方开户行及账号
            a.InfoSellerAddressPhone=options.kpdz + " " + options.kpdh;// - 销方地址电话


            a.InfoTaxRate=options.shuilv;// - 税率，17、13、6、4等
            a.InfoNotes="";// - 备注
            a.InfoInvoicer=options.kpr;//开票人
            a.InfoChecker=options.fhr;// - 复核人，可为空
            a.InfoCashier=options.skr;// - 收款人，可为空
            a.InfoListName="";//document.all.InfoListName.value;// - 如不为空，则开具销货清单，此为发票上商品名称栏的清单信息，应为"(详见销货清单)"字样
            a.InfoBillNumber="";//document.all.InfoBillNumber.value;// - 销售单据编号，可为空

            //发票明细超过8行，将自动开具销货清单
            a.ClearInvList();
            a.InvListInit();

            a.ListGoodsName =options.spmc;//货物名称(商品名称)
            a.ListTaxItem ="";//税目，4 位数字，商品所属类别
            a.ListStandard = "";//规格型号
            a.ListUnit =options.dw;//计量单位，如计量单位为空，则忽略数量和单价
            a.ListNumber = options.sl;//数量
            a.ListPrice = options.dj;//单价
            a.ListPriceKind = 1;//含税价标志，单价和金额的种类， 0 为不含税价，1 为含税价
            a.ListAmount = options.fee;//金额
            // a.ListTaxAmount ="";//税额，可以不传（为 0），由接口软件计算，如传入则应符合计算关系
            var SJBW = "<?xml version=\"1.0\" encoding=\"GBK\"?>" +
                "<FPXT>" +
                "<INPUT>" +
                "<GoodsNo>" +
                "<GoodsNoVer>39.0</GoodsNoVer>" +//必填编码版本号
                "<GoodsTaxNo>110030101</GoodsTaxNo>" +//税收分类编码
                "<TaxPre>0</TaxPre>" +
                "<TaxPreCon></TaxPreCon>" +
                "<ZeroTax></ZeroTax>" +
                "<CropGoodsNo>110030101</CropGoodsNo>" +//企业自编码(可自编)
                "<TaxDeduction></TaxDeduction>" +
                "</GoodsNo>" +
                "</INPUT>" +
                "</FPXT>";
            var strBase = this.encode64(SJBW);
            var YWBW = "<?xml version=\"1.0\" encoding=\"GBK\"?>" +
                "<FPXT_COM_INPUT>" +
                "<ID>1100</ID>" +
                "<DATA>" + strBase + "</DATA>" +
                "</FPXT_COM_INPUT>";
            a.BatchUpload(YWBW);//税收分类编码设置
            a.AddInvList();

            a.Invoice();

            if(a.RetCode=="4011"){
                var mess="开票成功！\n\n";
                mess+="发票整体信息如下：\n";
                mess+="合计不含税金额:"+a.InfoAmount;
                mess+="\n合计税额:"+a.InfoTaxAmount;
                mess+="\n开票日期:"+a.InfoDate;
                mess+="\n所属月份:"+a.InfoMonth;
                mess+="\n发票十位代码:"+a.InfoTypeCode;
                mess+="\n发票号码:"+a.InfoNumber;
                mess+="\n销货清单标志:"+a.GoodsListFlag;
                mess+="\n单价:"+a.ListPrice;
                mess+="\n数量:"+a.ListNumber;

                // options.items[0].taxAmount = a.InfoTaxAmount;
                console.log(mess);
                // console.log('开票后的数据!' + JSON.stringify(options));
                return true
            }else{
                console.log("开票失败，错误代码："+a.RetCode + a.RetMsg);
                alert("开票失败："+a.RetMsg);
                return false
            }
        }catch (e){
            alert("开票失败，："+ JSON.stringify(e));
            console.log(e);
        }finally {
            CloseCard();
        }
        return false
    }

    function getQuantity(invoiceType){
        var a = new ActiveXObject("TaxCardX.GoldTax");
        a.InfoKind=invoiceType;
        a.GetInfo();

        console.info('剩余发票数量:' + a.InvStock)

        return a.InvStock;
    }

    function encode64(input) {
        var keyStr = "ABCDEFGHIJKLMNOP" +
            "QRSTUVWXYZabcdef" +
            "ghijklmnopqrstuv" +
            "wxyz0123456789+/" +
            "=";

        input = escape(input);
        var output = "";
        var chr1, chr2, chr3 = "";
        var enc1, enc2, enc3, enc4 = "";
        var i = 0;
        do {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
            output = output +
                keyStr.charAt(enc1) +
                keyStr.charAt(enc2) +
                keyStr.charAt(enc3) +
                keyStr.charAt(enc4);
            chr1 = chr2 = chr3 = "";
            enc1 = enc2 = enc3 = enc4 = "";
        } while (i < input.length);

        return output;
    }
</script>
</html>